{% load static %}
{% load permission_tags %}

<div class="group_perm_manage" data-tab-id="group_perm_manage">

<!-- 在模板中安全注入数据 -->
{{ users|json_script:"usersData" }}
{{ groups|json_script:"groupsData" }}
{{ permissions|json_script:"permissionsData" }}

<div
    class="container-fluid py-1 bg-grep text-white fixed-area"
    style="width: 65vw; margin: 0 auto; font-size: 16px;"
    data-module-root="group-perm-manage">
    <div class="rbac-manager container-fluid py-2">
        <div class="glassmorphism-container p-2 rounded-4 shadow-lg">
            <!-- 头部操作栏 -->
            <div class="d-flex justify-content-between align-items-center mb-2" style="height: 20px">
                <h6 class="text-success fw-bold ms-2 mb-0"><i class = "bi bi-sliders fs-6"></i> 组权分配</h6>
                <button class="btn btn-outline-primary btn-sm fw-bold me-1" id="confirmBtn" style="font-size: calc(1rem * 0.75);">
                    <i class="bi bi-save me-1"></i>确认配置
                </button>
            </div>

            <!-- 二栏布局 -->
            <div class="row g-2">
                <!-- 组列 -->
                <div class="col-12 col-lg-4">
                    <div class="card h-100 border-0 glassmorphism-card">
                        <div class="card-body">
                            <div class="search-box mb-3 d-flex align-items-center">
                                <input type="text"
                                   class="form-control form-control-sm search-input"
                                   placeholder="🔍 搜索组..."
                                   data-search-type="group"
                                   style="height: 40px"
                                   id="groupSearch">
                                <button class="btn btn-outline-primary btn-sm ms-2 fw-bold" style="font-size: 15px; width: 120px">
                                    <i class="bi bi-save"></i> 新增组
                                </button>
                            </div>
                            <div class="group-list list-group list-group-flush" id="groupList_gpm"
                                 style="white-space: nowrap; overflow-x: hidden;font-size: calc(1rem * 0.95);">
                                {% for group in groups %}
                                    <div class="list-group-item d-flex align-items-center" data-userid="groupName_{{ group.id }}">
                                        <div class="form-check flex-grow-1">
                                            <input type="radio"
                                                   class="form-check-input group-radio gpm_radio_input"
                                                   value="{{ group.id }}"
                                                   id="gpm_group_{{ group.id }}"
                                                   name="groupList_gpm">
                                              <label class="form-check-label d-flex justify-content-between align-items-center w-100" for="gpm_group_{{ group.id }}">
                                                  <!-- 左侧区域：徽章+组名 -->
                                                  <span class="d-flex align-items-center">
                                                    <span class="badge bg-success btn-sm custom-gap">组</span>
                                                    <span id="groupName_{{ group.id }}" class="group-name text-truncate pe-2" style="max-width: 150px;">
                                                      {{ group.name }}
                                                    </span>
                                                  </span>

                                                  <!-- 右侧按钮组 -->
                                                  <span class="btn-group btn-group-wrap ms-auto" data-userid="groupName_{{ group.id }}">
                                                    <button class="btn btn-sm btn-outline-primary gpm_edit-btn"
                                                            title="编辑"
                                                            style="font-size: 12px"
                                                            data-userid="groupName_{{ group.id }}">
                                                      <i class="fas fa-edit"></i>
                                                    </button>
                                                    <button class="btn btn-sm btn-outline-danger delete-btn"
                                                            title="删除"
                                                            style="font-size: 12px"
                                                            data-userid="groupName_{{ group.id }}">
                                                      <i class="fas fa-trash"></i>
                                                    </button>
                                                  </span>
                                                </label>
                                        </div>
                                    </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 新权限 -->
                <div class="col-12 col-lg-8">
                    <div class="card h-100 border-0 shadow-sm" style="background: rgba(255, 255, 255, 0.2); backdrop-filter: blur(10px);">
                        <div class="card-body pt-1 pb-0 d-flex flex-column">
                            {% regroup permissions|dictsort:"codename" by codename|get_permission_group as permission_groups %}
                            <div class="flex-grow-1 overflow-auto scroll-container_gpm" id="scroll-container_gpm">
                            <ul class="list-unstyled mb-0" style="line-height: 1.1;">
                                {% for group in permission_groups %}
                                    {% with base=group.grouper %}
                                        <!-- 一级权限 -->
                                        <li class="mb-1">
                                            <div class="form-check form-switch d-flex align-items-center gap-2">
                                                <label class="form-check-label fw-bold text-info" for="gpm_perm_{{ group.list.0.id }}">
                                                <input class="form-check-input border-2 flex-shrink-0 gpm-checkbox-1 gpm_radio_input" type="checkbox"
                                                    id="gpm_perm_{{ group.list.0.id }}"
                                                    value="{{ group.list.0.codename }}"
                                                    data-display-toggle="">
                                                    <i class="bi bi-shield me-1"></i>{{ group.list.0.name }}
                                                </label>
                                            </div>
                                            <ul class="list-unstyled ps-3 mt-1">
                                                {% regroup group.list|slice:"1:" by codename|get_permission_level:1 as sub_groups %}
                                                {% for sub_group in sub_groups %}
                                                    <!-- 二级权限 -->
                                                    <li class="pt-0 d-flex align-items-center gap-2">
                                                        <div class="form-check">
                                                            <label class="form-check-label small fw-medium  text-black" for="gpm_perm_{{ sub_group.list.0.id }}">
                                                            <input class="form-check-input border-2 flex-shrink-0 gpm-checkbox-2 gpm_radio_input" type="checkbox"
                                                                id="gpm_perm_{{ sub_group.list.0.id }}"
                                                                value="{{ sub_group.list.0.codename }}"
                                                                data-display-toggle="">
                                                                <i class="bi bi-shield-shaded me-1"></i>{{ sub_group.list.0.name }}
                                                            </label>
                                                        </div>

                                                        <ul class="list-unstyled ps-2 mt-1 d-flex flex-wrap gap-1">
                                                            {% for perm in sub_group.list %}
                                                                {% if perm.codename|has_permission_level:2 %}
                                                                <!-- 三级权限 -->
                                                                <li class="list-inline-item">
                                                                    <div class="form-check form-check-inline gap-1">
                                                                        <label class="form-check-label small" style="color: #6c757d;" for="gpm_perm_{{ perm.id }}">
                                                                        <input class="form-check-input border-2 flex-shrink-0 gpm-checkbox-3 gpm_radio_input" type="checkbox"
                                                                            id="gpm_perm_{{ perm.id }}"
                                                                            value="{{ perm.codename }}"
                                                                            data-display-toggle="">
                                                                            {{ perm.name }}
                                                                        </label>
                                                                    </div>
                                                                </li>
                                                                {% endif %}
                                                            {% endfor %}
                                                        </ul>
                                                    </li>
                                                {% endfor %}
                                            </ul>
                                        </li>
                                    {% endwith %}
                                {% endfor %}
                            </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>


<!-- 提示小模态框 -->
{#<div class="modal fade" id="warningModal_gpm" tabindex="-1" data-bs-backdrop="static">#}
{#  <div class="modal-dialog modal-sm">#}
{#    <div class="modal-content" style="background-color: #ffe6f0; border: 2px solid #ff99c2;">#}
{#      <div class="modal-body text-center py-1">#}
{#        <p class="mt-1 mb-1 text-dark" style="font-size: 20px;" id="dynamicMessage_gpm">#}
{#          需要提示的信息:message变量#}
{#        </p>#}
{#      </div>#}
{#    </div>#}
{#  </div>#}
{#</div>#}


<!-- 删除模态框 -->
<div class="modal fade" id="deleteConfirmModal_gpm">
    <div class="modal-dialog modal-sm">
        <div class="modal-content" style="max-width: 300px; border: 2px solid #ffb3d1; border-radius: 8px; overflow: hidden">
            <!-- 内容 -->
            <div class="modal-body py-3 pb-1" style="background: #ffeff5; color: #c66988; font-size: 15px; padding: 0 2px 0 8px">
                <i class="fas fa-exclamation-triangle text-danger me-2"></i>
                确定要删除该组名吗？此操作不可逆！
            </div>
            <!-- 底部按钮 -->
            <div class="modal-footer py-1" style="background: #ffeff5; border-color: #ffeff5">
                <button type="button"
                        class="btn btn-sm px-3"
                        style="background: #ffffff; color: #d81b60; border: 1px solid #ffb3d1"
                        data-bs-dismiss="modal">
                    取消
                </button>
                <button type="button"
                        class="btn btn-sm px-3"
                        id="confirmDelete"
                        style="background: #ff5c8d; color: white; border: none">
                    <i class="fas fa-trash-alt me-1"></i>
                    确认删除
                </button>
            </div>
        </div>
    </div>
</div>
</div>

<style>
    [data-tab-id="group_perm_manage"] .fixed-area {
        position: absolute;
        top: 45px;
        bottom: 5px;
        left: 0;
        right: 0;
        overflow-y: auto; /* 支持内部滚动 */
    }

    [data-tab-id="group_perm_manage"] .glassmorphism-container {
        background: rgba(255, 255, 255, 0.85);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
    }

    [data-tab-id="group_perm_manage"] .list-group-item {
        transition: all 0.3s ease;
        border-radius: 8px !important;
        margin-bottom: 8px;
        background: rgba(255, 255, 255, 0.5);
    }

    [data-tab-id="group_perm_manage"] .list-group-item:hover {
        transform: translateX(5px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    [data-tab-id="group_perm_manage"] .search-input {
        border-radius: 20px;
        padding: 12px 20px;
        border: 1px solid rgba(0, 0, 0, 0.1);
    }

    [data-tab-id="group_perm_manage"] .search-input:focus {
        box-shadow: 0 0 0 3px rgba(92, 106, 196, 0.1);
    }

    [data-tab-id="group_perm_manage"] .badge {
        font-weight: 500;
        padding: 0.5em 0.75em;
    }

    [data-tab-id="group_perm_manage"] [data-source="group"] + label {
        opacity: 0.7;
        cursor: not-allowed;
    }

    [data-tab-id="group_perm_manage"] .perm-category h4 {
        border-left: 4px solid;
        padding-left: 0.5rem;
    }

    [data-tab-id="group_perm_manage"] #directPerms h4 {
        border-color: #0d6efd;
    }

    /* 蓝色 */
    [data-tab-id="group_perm_manage"] #groupPerms h4 {
        border-color: #198754;
    }

    /* 添加自定义样式 */
    [data-tab-id="group_perm_manage"] .card {
        min-height: 400px; /* 设置最小高度确保父容器有基准 */
    }

    [data-tab-id="group_perm_manage"] .scroll-container_gpm {
        flex: 1 1 auto;
        min-height: 0; /* 修复Safari浏览器的flex布局bug */
        overflow-y: auto; /* 垂直滚动 */
        -webkit-overflow-scrolling: touch; /* iOS滚动优化 */
    }

    [data-tab-id="group_perm_manage"] .group-name {
      background-color: transparent;
      border: none;
      width: 150px;
     }

    /* 自定义2px间距 */
    [data-tab-id="group_perm_manage"] .custom-gap { margin-right: 2px!important; }
     /* 防止按钮组换行 */
    [data-tab-id="group_perm_manage"] .btn-group-wrap { white-space: nowrap; }

    /* 新建专属高亮类 */
    [data-tab-id="group_perm_manage"] .list-group-highlight {
      background-color: #f8d7da !important;
      border-color: #f1aeb5 !important;
    }
    [data-tab-id="group_perm_manage"] .list-group-highlight .form-check,
    [data-tab-id="group_perm_manage"] .list-group-highlight .form-check-label {
      background-color: transparent !important;
    }
</style>



